const express = require('express');
const path = require('path');

let http = require('http');
let loginRouter = require('./router/login');

let app = express();
app.use(require('body-parser').json());

/*
//test code
app.use((req, res, next) => {
    console.log('1');
    next();   //只有next调用了，才会触发下一个注册了use的function
});
app.use((req, res, next) => {
    console.log('2');
    next();
});
*/

//express.static 使用静态的文件如梦html、image等 '/static' 虚拟目录不存在的，url中不包括pulic，按use顺序查找文件
app.use('/static', express.static(path.join(__dirname + '/public')));
//app.use('/static', express.static(path.join(__dirname + '/public2')));

app.use('/login', loginRouter);

let server = http.createServer(app);
server.listen(3000);
server.on('error', (err) => {

});
server.on('listening', (code) => {
    let addr = server.address();
    console.log('listen successfully. addr: ' + JSON.stringify(addr));
});
